import {Context} from "../../context";
import {makeId} from "../../idMaker";
import * as WebSocket from 'ws';

export class ContextManager {
  private ctxs: Map<WebSocket, Context> = new Map<WebSocket, Context>();
  private roleCtxMap = new Map<number, Context>();

  createContext(ws: WebSocket) {
    const ctx = new Context();
    ctx.id = makeId();
    ctx.ws = ws;
    this.ctxs.set(ws, ctx);
    return ctx
  }

  getCtxByWs(ws: WebSocket): Context {
    return this.ctxs.get(ws);
  }

  isOnline(id: number) {
    return this.roleCtxMap.has(id);
  }

  offline(ws: WebSocket) {
    this.ctxs.delete(ws);
  }
}